package com.demo.apparel.mapper.uniapp;

import com.demo.apparel.entity.VisitCount;
import com.demo.apparel.entity.VisitCountDaily;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface VisitCountMapper {

    @Select("SELECT * FROM visit_count WHERE uri = #{uri}")
    VisitCount getByUri(String uri);

    @Insert("INSERT INTO visit_count(uri, count, last_visit_time) VALUES(#{uri}, 1, NOW())")
    int insert(VisitCount visitCount);

    @Update("UPDATE visit_count SET count = count + 1, last_visit_time = NOW() WHERE uri = #{uri}")
    int incrementCount(String uri);

    @Insert("""
        INSERT INTO visit_count_daily (uri, visit_date, count)
        VALUES (#{uri}, #{visitDate}, 1)
        ON DUPLICATE KEY UPDATE count = count + 1
    """)
    void insertOrUpdate(String uri, LocalDate visitDate);

    @Select("SELECT * FROM visit_count_daily WHERE visit_date = #{visitDate}")
    List<VisitCountDaily> findByDate(LocalDate visitDate);
}
